//
//  LQ_adLoadingLayer.cpp
//  Cupcake
//
//  Created by luotianqiang1 on 15/6/15.
//
//

#include "LQ_adLoadingLayer.h"
#include "KeyBackEvent.h"
#include<iostream>
#include "StudioLayer.h"
#include "cocostudio/cocostudio.h"
//#include "ChangeLoading.h"

bool LQ_adLoadingLayer::isChangeLayer = true;
using namespace std;
bool LQ_adLoadingLayer::init(){
    if(AdLoadingLayerBase::init()) {
        _loadingMinTime = 0;
        _loadingMaxTime = 3;
        
        auto _operate = StudioLayer::create("LoadingLayer.csb");
        this->addChild(_operate);
        for(int i=0;i<3;i++)
            dots.push_back(_operate->getNodeByName<Sprite*>(StringUtils::format("dian%d",i)));
        Vector<FiniteTimeAction*> actions;
        for(int i=0;i<dots.size();i++){
             dots.at(i)->setVisible(false);
            actions.pushBack(CallFunc::create([=](){
                dots.at(i)->setVisible(true);
                dots.at(i)->setScale(0);
                dots.at(i)->runAction(ScaleTo::create(0.1, 1));}));
            actions.pushBack(DelayTime::create(0.2));
        }
        actions.pushBack(DelayTime::create(0.5));
        actions.pushBack(CallFunc::create([this](){
            for(auto dot:dots)
                dot->runAction(ScaleTo::create(0, 0));
        }));
        actions.pushBack(DelayTime::create(1.2));
        this->runAction(RepeatForever::create(Sequence::create(actions)));
        
        return true;
        
        auto* contentLayer = Layer::create();
        contentLayer->ignoreAnchorPointForPosition(false);
        contentLayer->setContentSize(Size(960,640));
        contentLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        contentLayer->setPosition(getContentSize()*.5);
        this->addChild(contentLayer);
        
        auto bg = Sprite::create("loading/bg.jpg");
        bg->setPosition(contentLayer->getContentSize()*.5);
        contentLayer->addChild(bg);
        float posX[] = {698.91,739.00,779.10};
        
        for(int i=0;i<3;i++){
            auto dot = Sprite::create("loading/d.png");
            dot->setPosition(posX[i],508.00);
            dot->setScale(0);
            dots.push_back(dot);
            contentLayer->addChild(dot);
        }
        tents = Sprite::create("loading/z.png");
        tents->setAnchorPoint(Vec2(0.5,0.25));
        tents->setPosition(480.00,141.75);
        //tents->setScale(1,0);
        contentLayer->addChild(tents);
//        tents->runAction(Sequence::create(EaseBackOut::create(ScaleTo::create(0.5, 1)),CallFunc::create([this](){
          //  Vector<FiniteTimeAction*> actions;
            for(int i=0;i<dots.size();i++){
                actions.pushBack(CallFunc::create([=](){dots.at(i)->runAction(ScaleTo::create(0.1, 1));}));
                actions.pushBack(DelayTime::create(0.2));
            }
            actions.pushBack(DelayTime::create(0.5));
            actions.pushBack(CallFunc::create([this](){
                for(auto dot:dots)
                    dot->runAction(ScaleTo::create(0, 0));
            }));
            actions.pushBack(DelayTime::create(1.2));
            this->runAction(RepeatForever::create(Sequence::create(actions)));
//        }),DelayTime::create(1),CallFunc::create([this](){
            float time = 0.25;
            tents->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(time*0.6, 1,1.03),ScaleTo::create(time, 1,0.96),Spawn::create(ScaleTo::create(time, 1),SkewTo::create(time,-7,0), nullptr),Spawn::create(Sequence::create(ScaleTo::create(time, 1,0.96),ScaleTo::create(time, 1), nullptr),SkewTo::create(time*2,7,0), nullptr),Spawn::create(ScaleTo::create(time, 1,0.96),SkewTo::create(time,0,0), nullptr),ScaleTo::create(time*0.6, 1),DelayTime::create(0.7), nullptr)));
  //      }), nullptr));
        
        
       
        return true;
    }
    return false;
}

void LQ_adLoadingLayer::onEnterTransitionDidFinish(){
    AdLoadingLayerBase::onEnterTransitionDidFinish();
//    removeAllChildren();
//    this->runAction(Sequence::create(DelayTime::create(0.1), CallFunc::create([this](){
//        if(!isChangeLayer)
//        {
////            _loadingMinTime = 0;
////            _loadingMaxTime = 5;
//            auto bg = Sprite::create("loading/loading.jpg");
//            bg-> setPosition(getContentSize()*.5);
//            addChild(bg);
//            cocostudio::ArmatureDataManager::getInstance() -> addArmatureFileInfo("loading/loading/loading.ExportJson");
//            cocostudio::Armature *armature = cocostudio::Armature::create("loading");
//        auto size = Director::getInstance()->getVisibleSize();
//            armature->setPosition(size*.5-Size(0,100));
//            armature->getAnimation()->playWithIndex(0);
//            this->addChild(armature);
//        }
//        else {
////            _loadingMinTime = 3.5;
////            _loadingMaxTime = 5;
//            addChild(ChangeLoading::create());
//        }
//        isChangeLayer = true;
//    }),nullptr));
   
    KeyBackEvent::getInstance()->addBackEvent([](){return true;}, this);
}
